package com.newsee.remote.ecommerce.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newsee.remote.common.database.entity.SystemParamvalue;
import com.newsee.remote.common.database.mapper.SystemParamvalueMapper;
import com.newsee.remote.ecommerce.service.ISystemParamvalueService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liangzhichen
 * @since 2019-08-28
 */
@Service
public class SystemParamvalueServiceImpl extends ServiceImpl<SystemParamvalueMapper, SystemParamvalue> implements ISystemParamvalueService {

    private final static Long ParamTypeID = 1060L;

    @Resource
    private SystemParamvalueMapper systemParamvalueMapper;

    @Override
    public String getUnit(String unit) {
        if (StringUtils.isEmpty(unit)) {
            return null;
        }

        String paramVale = null;
        QueryWrapper<SystemParamvalue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ParamValueName", unit).eq("ParamTypeID", ParamTypeID);
        SystemParamvalue systemParamvalue = getOne(queryWrapper);

        //有改单位的基础系统仓数
        if (null != systemParamvalue && StringUtils.isNotEmpty(systemParamvalue.getParamValue())) {
            paramVale = systemParamvalue.getParamValue();
        }
        //新增
        else {
            SystemParamvalue po = new SystemParamvalue();
            Long max = systemParamvalueMapper.getMax();
            if (null != max) {
                max += 1;
                //ParamValueID, ParamTypeID, ParamValueName, ParamValue
                po.setParamValueID(max);
                po.setParamTypeID(ParamTypeID);
                po.setParamValueName(unit);
                po.setParamValue(max.toString());
                save(po);
                paramVale = max.toString();
            }
        }
        return paramVale;
    }

    @Override
    public String getUnitName(String ParamValue) {
        try {
            QueryWrapper<SystemParamvalue> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("ParamValue", ParamValue).eq("ParamTypeID", ParamTypeID);
            SystemParamvalue systemParamvalue = getOne(queryWrapper);
            return systemParamvalue.getParamValueName();
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }
    }
}
